package ms.utils;

import java.util.Iterator;

import ms.jasim.model.JasimTemplate;
import ms.jasim.model.ModelObject;
import ms.jasim.model.NamedModelObject;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.impl.EObjectImpl;

public class EObjectListImpl<T extends NamedModelObject> extends EObjectImpl
implements IModelObjectList<T>, ModelObject {

	protected EList<T> list;
	private String name;
	private boolean predefined;
	private boolean readonly;

	public EObjectListImpl() {

	}

	public EObjectListImpl(EList<T> list) {
		setInternalList(list);
	}

	protected void setInternalList(EList<T> list) {
		this.list = list;
	}

	public T get(int index) {
		return list != null ? list.get(index) : null;
	}

	public T get(String name) {
		int idx = indexOf(name);
		return idx >= 0 ? list.get(idx) : null;
	}

	public EList<T> getList() {
		return list;
	}

	public int indexOf(String name) {
		if (list != null)
			for (int i = 0; i < list.size(); i++)
				if (list.get(i).getName().equalsIgnoreCase(name))
					return i;
		return -1;
	}

	@Override
	public Iterator<T> iterator() {
		return list != null ? list.iterator() : null;
	}

	@Override
	public int indexOf(T object) {
		return list != null ? list.indexOf(object) : null;
	}

	@Override
	public int size() {
		return list != null ? list.size() : 0;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return name;
	}

	
	@Override
	public boolean isPredefined() {
		return this.predefined;
	}

	@Override
	public void setName(String value) {
		name = value;
	}

	@Override
	public void setPredefined(boolean value) {
		predefined = value;
	}

	
	@Override
	public boolean isReadonly() {
		return this.readonly;
	}

	@Override
	
	public boolean isTemplateObject() {
		return getRootObject() instanceof JasimTemplate;
	}

	
	@Override
	public void setReadonly(boolean value) {
		this.readonly = value;		
	}

	protected ModelObject getRootObject() {
		ModelObject res = this;
		while (res.eContainer() != null)
			res = (ModelObject) res.eContainer();
		return res;
	}

	
	@Override
	public boolean isReservedName() {
		// TODO Auto-generated method stub
		return false;
	}

	
	@Override
	public void setReservedName(boolean value) {
		// TODO Auto-generated method stub
		
	}
}
